﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;

namespace WcfContrib.Metadata
{
    /// <summary>
    /// Represents the summary description for a service metadata.
    /// </summary>
    public sealed class MetadataDescription
    {
        /// <summary>
        /// Gets the collection of endpoints summary description imported from the metadata
        /// </summary>
        public ReadOnlyCollection<MetadataEndpointDescription> Endpoints { get; internal set; }

        /// <summary>
        /// Get the metadata exchange client used to retrieve the metadata
        /// </summary>
        public MetadataExchangeClient MexClient { get; internal set; }

        /// <summary>
        /// Gets the metadata set imported from the metadata
        /// </summary>
        public MetadataSet MetadataSet { get; internal set; }

        /// <summary>
        /// Gets the metadata importer used to retrieve the metadata
        /// </summary>
        public MetadataImporter MetadataImporter { get; internal set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MetadataDescription"/> class.
        /// </summary>
        public MetadataDescription()
        {

        }

        internal void PopulateEndpoints(ServiceEndpointCollection endpoints)
        {
            List<MetadataEndpointDescription> eps = new List<MetadataEndpointDescription>(endpoints.Count);

            foreach (ServiceEndpoint ep in endpoints)
            {
                MetadataEndpointDescription epDesc = new MetadataEndpointDescription()
                {
                    Endpoint = ep,
                    Policies = PolicyTagger.GetPolicies(ep)
                };

                eps.Add(epDesc);
            }

            Endpoints = eps.AsReadOnly();
        }
    }
}
